<template>
  <div class="user-wrapper">
    <div class="content-box">
      <a-dropdown>
        <span class="action ant-dropdown-link user-dropdown-menu">
          <a-avatar class="avatar" size="small" :src="avatar()"/>
          <span>{{ nickname() }}</span>
        </span>
        <a-menu slot="overlay" class="user-dropdown-menu-wrapper">
          <a-menu-item key="1">
            <div @click="handleChangePwd">
              <a-icon type="setting"/>
              <span>修改密码</span>
            </div>
          </a-menu-item>
          <a-menu-divider/>
          <a-menu-item key="3">
            <a href="javascript:;" @click="handleLogout">
              <a-icon type="logout"/>
              <span>退出登录</span>
            </a>
          </a-menu-item>
        </a-menu>
      </a-dropdown>
    </div>
    <Modal :modalOptions="modalOptions" @handleOk="handleSubmit" @handleCancel="handleCancel">
      <div slot="modalCon">
        <a-form
          :form="form"
        >
          <a-form-item
            label="旧密码"
            v-bind="formItemLayout"
          >
            <a-input
              v-decorator="['oldPassword', {
                rules: []
              }]"/>
          </a-form-item>
          <a-form-item
            label="新密码"
            v-bind="formItemLayout"
          >
            <a-input
              v-decorator="['newPassword', {
                rules: []
              }]" />
          </a-form-item>
          <!-- <a-form-item
            label="确认密码"
            v-bind="formItemLayout"
          >
            <a-input
              v-decorator="['oldPassword', {
                rules: []
              }]" />
          </a-form-item> -->
        </a-form>
      </div>
    </Modal>
  </div>
</template>

<script>
import { mapActions, mapGetters } from 'vuex'
import Modal from '@/components/Modal'
import { handleUserChange } from '@/server/api'
export default {
  name: 'UserMenu',
  data () {
    return {
      formItemLayout: {
        labelCol: {
          xs: { span: 4 },
          sm: { span: 4 }
        },
        wrapperCol: {
          xs: { span: 4 },
          sm: { span: 16 }
        }
      },
      modalOptions: {
        title: '修改密码',
        modalVisibile: false
      }
    }
  },
  created () {
    this.form = this.$form.createForm(this)
  },
  components: {
    Modal
  },
  methods: {
    ...mapActions(['Logout']),
    ...mapGetters(['nickname', 'avatar']),
    handleLogout () {
      const that = this

      this.$confirm({
        title: '提示',
        content: '真的要注销登录吗 ?',
        onOk () {
          return that.Logout({}).then(() => {
            window.location.reload()
          }).catch(err => {
            that.$message.error({
              title: '错误',
              description: err.message
            })
          })
        },
        onCancel () {
        }
      })
    },
    handleChangePwd () {
      this.modalOptions.modalVisibile = true
    },
    handleSubmit () {
      this.form.validateFields((error, fieldsValue) => {
        const params = {
          ...fieldsValue
        }
        console.log(params, '==params')
        handleUserChange(params).then(res => {
          if (res.code === 0) {
            this.$message.success('修改成功')
            this.$router.push({ name: 'login' })
          }
        })
      })
    }
  }
}
</script>
